package com.handle;

import com.channel.ChannelContent;
import com.message.notify.NotifyType;
import com.message.notify.RegisterNotifyMessage;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author linyu.dai
 * @project rpc
 * @description 用来处理注册中心 通知消息
 * @creat 2022/11/29 11:16:12
 */
@Slf4j
@ChannelHandler.Sharable
public class RegisterNotifyHandle extends SimpleChannelInboundHandler<RegisterNotifyMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RegisterNotifyMessage msg) throws Exception {
        if(NotifyType.OFFLINE == msg.getNotifyType()){
            log.info("{}服务已经下线", msg.getServerName());
            ChannelContent.removeChannel(msg.getServerName());
        }

        if(NotifyType.ONLINE == msg.getNotifyType()){
            log.info("{}服务已经上线", msg.getServerName());
            ChannelContent.addChannel(msg.getServerName(), msg.getHost());
        }

    }
}
